package com.zyw.controller.repertory;


import com.zyw.entity.PersonnelDepartment;
import com.zyw.entity.PersonnelStaff;
import com.zyw.entity.RepertoryMaterial;
import com.zyw.entity.RepertoryMaterialLog;
import com.zyw.from.personnelFrom;
import com.zyw.from.repertoryFrom;
import com.zyw.service.repertory.RepertoryMaterialLogService;
import com.zyw.service.repertory.RepertoryMaterialService;
import com.zyw.util.ResultVOUtil;
import com.zyw.vo.LayUITableVO;
import com.zyw.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zyw.constant.ManagermentSystemConstant.MATERIAL_REPERTORY_NUM_MAP;

@Controller
@Slf4j
@RequestMapping("/repertory/material")
public class RepertoryMaterialController {


    @Autowired
    public RepertoryMaterialService repertoryMaterialService;

    @Autowired
    public RepertoryMaterialLogService repertoryMaterialLogService;

    //菜品类别展示
    @GetMapping("/index")
    public String index(Model model){


        List<RepertoryMaterial> repertoryMaterialList = repertoryMaterialService.selectParent();

        model.addAttribute("repertoryMaterialList",repertoryMaterialList);
        return "repertory/material/index";

    }
    //获取菜品类别信息
    @GetMapping("/list")
    @ResponseBody
    public LayUITableVO<List<RepertoryMaterial>> list (@RequestParam(defaultValue = "1") Integer page,
                                                    @RequestParam(defaultValue = "10") Integer limit,
                                                    repertoryFrom from){

        Map<String ,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);
        LayUITableVO< List<RepertoryMaterial>> vo = repertoryMaterialService.findPage(map);


        return vo;
    }
    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/edit-ui")
    public String editUI(String id,Model model){

        RepertoryMaterial repertoryMaterial = repertoryMaterialService.selectById(id);

        List<RepertoryMaterial> repertoryMaterialList = repertoryMaterialService.selectParent();

        model.addAttribute("repertoryMaterialList",repertoryMaterialList);

        model.addAttribute("repertoryMaterial",repertoryMaterial);

        return "repertory/material/edit";

    }

    //执行修改
    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(RepertoryMaterial repertoryMaterial){

        repertoryMaterialService.update(repertoryMaterial);

        return ResultVOUtil.ok();
    }

    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/add-ui")
    public String addUI(Model model){

        List<RepertoryMaterial> repertoryMaterialList = repertoryMaterialService.selectParent();

        model.addAttribute("repertoryMaterialList",repertoryMaterialList);

        return "repertory/material/add";

    }
    //执行修改
    @PostMapping("/add")
    @ResponseBody
    public ResultVO add(RepertoryMaterial repertoryMaterial){

        repertoryMaterialService.save(repertoryMaterial);

        return ResultVOUtil.ok();
    }
    //执行删除
    @PostMapping("/del")
    @ResponseBody
    public ResultVO del(String id){
        repertoryMaterialService.delelteById(id);
        return ResultVOUtil.ok();
    }

    //菜品类别展示
    @GetMapping("/repertoryIndex")
    public String repertoryIndex(Model model){


        List<RepertoryMaterial> repertoryMaterialList = repertoryMaterialService.selectParent();

        model.addAttribute("repertoryMaterialList",repertoryMaterialList);
        return "repertory/materialRepertory/index";

    }

    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/repertoryEdit-ui")
    public String repertoryEditUI(String id,Model model){

        RepertoryMaterial repertoryMaterial = repertoryMaterialService.selectById(id);
        MATERIAL_REPERTORY_NUM_MAP.put(Integer.valueOf(id),repertoryMaterial.getRepertoryNum());


        model.addAttribute("repertoryMaterial",repertoryMaterial);

        return "repertory/materialRepertory/edit";

    }

    //执行修改
    @PostMapping("/repertoryEdit")
    @ResponseBody
    public ResultVO repertoryEdit(RepertoryMaterial repertoryMaterial){

        repertoryMaterialService.update(repertoryMaterial);
        int i = MATERIAL_REPERTORY_NUM_MAP.get(repertoryMaterial.getId());
        int repertoryNum = repertoryMaterial.getRepertoryNum();
        if (repertoryNum != i) {

            RepertoryMaterialLog repertoryMaterialLog = new RepertoryMaterialLog();
            repertoryMaterialLog.setMaterialId(repertoryMaterial.getId());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            repertoryMaterialLog.setDate(simpleDateFormat.format(new Date()));
            //TODO 登录人名称
            repertoryMaterialLog.setOperator("小绿");
            repertoryMaterialLog.setOperatorType("物料库存编辑");
            repertoryMaterialLog.setBeforeRevision(MATERIAL_REPERTORY_NUM_MAP.get(repertoryMaterial.getId()));
            repertoryMaterialLog.setChangeNum(repertoryMaterial.getRepertoryNum() - repertoryMaterialLog.getBeforeRevision());

            repertoryMaterialLogService.save(repertoryMaterialLog);
        }
        return ResultVOUtil.ok();
    }



    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/repertoryShow")
    public String repertoryShow(String id,Model model){


        model.addAttribute("id",id);

        return "repertory/materialRepertory/show";

    }

    //获取菜品类别信息
    @GetMapping("/logList")
    @ResponseBody
    public LayUITableVO<List<RepertoryMaterialLog>> logList (@RequestParam(defaultValue = "1") Integer page,
                                                       @RequestParam(defaultValue = "10") Integer limit,
                                                       repertoryFrom from){

        Map<String ,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);
        LayUITableVO< List<RepertoryMaterialLog>> vo = repertoryMaterialLogService.findPage(map);


        return vo;
    }



}
